package com.kamistoat.meimeimall.meimeimallproduct.dubbo;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeimall.common.To.productTo.SkuCartInfoTo;
import com.kamistoat.meimeimall.common.To.productTo.SpuInfoTo;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillSkuInfoTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.dubboInterface.ProductDubboInterface;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallproduct.dao.SkuSaleAttrValueDao;
import com.kamistoat.meimeimall.meimeimallproduct.entity.SkuInfoEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.*;
import io.seata.core.constants.DubboConstants;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Dubbo接口实现
 */
@Service
@Component
public class ProductDubboInterfaceImpl implements ProductDubboInterface {
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    BrandService brandService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    CategoryService categoryService;

    /**
     * 获取商品的简易信息。构建购物车用
     *
     * @param skuId skuId
     * @return R. 内含 "skuCartInfoTo" - SkuCartInfoTo 对象
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getSkuBizInfoForCart(Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        SkuCartInfoTo skuCartInfoTo = new SkuCartInfoTo();
        BeanUtils.copyProperties(skuInfo, skuCartInfoTo);
        skuCartInfoTo.setBrandName(brandService.getById(skuInfo.getBrandId()).getName());
        return R.ok().put("skuCartInfoTo", JSON.toJSONString(skuCartInfoTo));
    }

    /**
     * 获取sku的详细信息。构建秒杀时用
     *
     * @param skuId skuId
     * @return R.内含 secKillSkuInfoTo
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getSkuInfoForSecKill(Long skuId) {
        SecKillSkuInfoTo secKillSkuInfoTo = skuInfoService.getSkuInfoForSecKill(skuId);
        return R.ok().put("secKillSkuInfoTo", JSON.toJSONString(secKillSkuInfoTo));
    }

    /**
     * 获取商品属性
     *
     * @param skuId skuId
     * @return R. 内含 "stringAttrList" -  List<String>
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getStringAttr(Long skuId) {
        List<String> stringAttrList = skuSaleAttrValueService.getStringAttr(skuId);
        return R.ok().put("stringAttrList", JSON.toJSONString(stringAttrList));
    }

    /**
     * 获取商品最新价格。
     *
     * @param skuId skuId
     * @return R. 内含 "price" - BigDecimal
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getNewestPrice(Long skuId) {
        BigDecimal price = skuInfoService.getById(skuId).getPrice();
        return R.ok().put("price", price);
    }

    /**
     * 根据skuId获取Spu
     *
     * @param skuId skuId
     * @return R. 内含 "spuInfoTo" - SpuInfoTo
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getSpuBySkuId(Long skuId) {
        Long spuId = skuInfoService.getById(skuId).getSpuId();
        SpuInfoTo spuInfoTo = spuInfoService.getSpuInfoById(spuId);
        return R.ok().put("spuInfoTo", JSON.toJSONString(spuInfoTo));
    }

    /**
     * 查询菜单的层级
     *
     * @param categoryId
     */
    @Override
    public R getCategoryLevel(Long categoryId) {
        int categoryLevel = categoryService.getCategoryLevel(categoryId);
        return R.ok().put("categoryLevel", categoryLevel);
    }
}
